import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Event;
import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import java.awt.GridBagLayout;
import javax.swing.JTabbedPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.GridBagConstraints;
import java.awt.Dimension;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;

public class MainFrame {
	public static final String ALL = "Tất cả";  //  @jve:decl-index=0:
	public static final int EDIT_LINE = 0;
	public static final int EDIT_STATION = 1;
	public static final int EDIT_ZONE = 2;
	
	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JDialog aboutDialog = null;
	private JPanel aboutContentPane = null;
	private JLabel aboutVersionLabel = null;
	private JPanel jPanel = null;
	private JTabbedPane jTabbedPane = null;
	private JPanel Xem = null;
	private DefaultListModel model111 = null;
	private JTextArea detail = null;
	private JPanel jPanel2 = null;
	private JPanel jPanel3 = null;
	private AutoCompletePanel busLinePanel1 = null;
	private JPanel jPanel4 = null;
	private JPanel jPanel5 = null;
	private JPanel jPanel6 = null;
	private JPanel jPanel7 = null;
	private JPanel jPanel8 = null;
	private JPanel jPanel9 = null;
	private JPanel jPanel10 = null;
	private AutoCompletePanel zonePanel = null;
	private AutoCompletePanel linePanel = null;
	private AutoCompletePanel stationPanel = null;
	private JPanel jPanel1 = null;
	private AutoCompletePanel startStation = null;
	private AutoCompletePanel endStation = null;
	private JPanel jPanel11 = null;
	private JPanel jPanel12 = null;
	private JButton findButton = null;
	private JTextArea jTextArea = null;
	private JPanel jPanel13 = null;
	private JComboBox jComboBox = null;
	private DefaultComboBoxModel typeModel = null;
	private JPanel editPanel = null;
	private EditLinePanel editLinePanel = null;
	private EditStationPanel editStationPanel = null;
	private EditZonePanel editZonePanel = null;
	private JButton jButton = null;
	private JLabel jLabel = null;
	private JTextArea lineDetailTextArea = null;
	private JTextArea stationDetailTextArea = null;
	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(591, 415);
			jFrame.setContentPane(getJPanel());
			jFrame.setTitle("Bus Transerco");
		}
		return jFrame;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog aboutDialog = getAboutDialog();
					aboutDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(20, 20);
					aboutDialog.setLocation(loc);
					aboutDialog.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes aboutDialog	
	 * 	
	 * @return javax.swing.JDialog
	 */
	private JDialog getAboutDialog() {
		if (aboutDialog == null) {
			aboutDialog = new JDialog(getJFrame(), true);
			aboutDialog.setTitle("About");
			aboutDialog.setContentPane(getAboutContentPane());
		}
		return aboutDialog;
	}

	/**
	 * This method initializes aboutContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAboutContentPane() {
		if (aboutContentPane == null) {
			aboutContentPane = new JPanel();
			aboutContentPane.setLayout(new BorderLayout());
			aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
		}
		return aboutContentPane;
	}

	/**
	 * This method initializes aboutVersionLabel	
	 * 	
	 * @return javax.swing.JLabel	
	 */
	private JLabel getAboutVersionLabel() {
		if (aboutVersionLabel == null) {
			aboutVersionLabel = new JLabel();
			aboutVersionLabel.setText("BusTranserco 1.0");
			aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return aboutVersionLabel;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJTabbedPane(), gridBagConstraints);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("Xem", null, getXem(), null);
			jTabbedPane.addTab("Tìm đường", null, getJPanel2(), null);
			jTabbedPane.addTab("Thêm - Sửa", null, getAddPanel(), null);
			jTabbedPane.addFocusListener(new FocusListener() {
				
				@Override
				public void focusLost(FocusEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void focusGained(FocusEvent e) {
					initZonePanel();
					initLinePanel();
					initStationsPanel();
					initStartStation();
					initEndStation();
				}
			});
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes Xem	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getXem() {
		if (Xem == null) {
			Xem = new JPanel();
			Xem.setLayout(new GridLayout(1,1));
			Xem.add(getJPanel4());
		}
		return Xem;
	}

	/**
	 * This method initializes model111	
	 * 	
	 * @return javax.swing.DefaultListModel	
	 */
	private DefaultListModel getModel111() {
		if (model111 == null) {
			model111 = new DefaultListModel();
		}
		return model111;
	}

	/**
	 * This method initializes detail	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getDetail() {
		if (detail == null) {
			detail = new JTextArea();
		}
		return detail;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(1);
			jPanel2 = new JPanel();
			jPanel2.setLayout(gridLayout);
			jPanel2.add(getJPanel1(), null);
			jPanel2.add(getJPanel11(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getAddPanel() {
		if (jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.setLayout(new BorderLayout());
			jPanel3.add(getJPanel13(), BorderLayout.NORTH);
			jPanel3.add(getEditLinePanel(), BorderLayout.CENTER);
			editPanel = getEditLinePanel();
		}
		return jPanel3;
	}

	/**
	 * This method initializes busLinePanel1	
	 * 	
	 * @return AutoCompletePanel	
	 */
	private AutoCompletePanel getBusLinePanel1() {
		if (busLinePanel1 == null) {
			busLinePanel1 = new AutoCompletePanel();
		}
		return busLinePanel1;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			jPanel4 = new JPanel();
			jPanel4.setLayout(new GridLayout(1, 2));
			jPanel4.add(getJPanel5(), null);
			jPanel4.add(getJPanel6(), null);
		}
		return jPanel4;
	}

	/**
	 * This method initializes jPanel5	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel5() {
		if (jPanel5 == null) {
			jPanel5 = new JPanel();
			jPanel5.setLayout(new GridLayout(1, 2));
			jPanel5.add(getJPanel7());
			jPanel5.add(getJPanel8(), null);
		}
		return jPanel5;
	}

	/**
	 * This method initializes jPanel6	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel6() {
		if (jPanel6 == null) {
			GridLayout gridLayout2 = new GridLayout();
			gridLayout2.setRows(2);
			gridLayout2.setColumns(1);
			jPanel6 = new JPanel();
			jPanel6.setLayout(gridLayout2);
			jPanel6.add(getLineDetailTextArea(), null);
			jPanel6.add(getStationDetailTextArea(), null);
		}
		return jPanel6;
	}

	/**
	 * This method initializes jPanel7	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel7() {
		if (jPanel7 == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.gridx = 0;
			jPanel7 = new JPanel();
			jPanel7.setLayout(new GridBagLayout());
			jPanel7.add(getZonePanel(), gridBagConstraints1);
		}
		return jPanel7;
	}

	/**
	 * This method initializes jPanel8	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel8() {
		if (jPanel8 == null) {
			jPanel8 = new JPanel();
			jPanel8.setLayout(new GridLayout(2, 1));
			jPanel8.add(getLinePanel(), null);
			jPanel8.add(getStationPanel(), null);
		}
		return jPanel8;
	}

	/**
	 * This method initializes jPanel9	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel9() {
		if (jPanel9 == null) {
			jPanel9 = new JPanel();
			jPanel9.setLayout(new GridLayout(1, 1));
			jPanel9.add(getLinePanel());
		}
		return jPanel9;
	}

	/**
	 * This method initializes jPanel10	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel10() {
		if (jPanel10 == null) {
			jPanel10 = new JPanel();
			jPanel10.setLayout(new GridLayout(1, 1));
			jPanel10.add(getStationPanel());
		}
		return jPanel10;
	}

	private void initZonePanel() {
		ArrayList<Item> items = new ArrayList<Item>();
		ZoneManager zm = null;
		try {
			zm = ZoneManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<Zone> zones = zm.getAllZone();
		items.add(new Item(0, ALL));
		for (Zone zone : zones) {
			Item item = new Item();
			item.id = zone.getId();
			item.name = zone.getName();
			items.add(item);
		}
		getZonePanel().setItems(items);
		getZonePanel().updateItemList();
	}
	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private AutoCompletePanel getZonePanel() {
		if (zonePanel == null) {
			zonePanel = new AutoCompletePanel();
			zonePanel.setBorder(new LineBorder(Color.BLACK));
			zonePanel.setLabel("Chọn khu vực:");
			
			initZonePanel();
			
			zonePanel.getItemList().addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {
					Item item = getZonePanel().getSelectedItem();
					if (item == null) return;
					BusLineManager bm = null;
					StationManager sm = null;
					try {
						bm = BusLineManager.getInstance();
						sm = StationManager.getInstance();
					} catch (Exception e1) {
						
					}
					ArrayList<Item> items = new ArrayList<Item>();
					ArrayList<BusLine> lines = null;
					if (item.name.equals(ALL)) {
						lines = bm.getAllBusLine();
					} else {
						lines = bm.getBusLineInZone(item.id);
					}
					for (BusLine line : lines) {
						items.add(new Item(line.getId(), line.getName()));
					}
					getLinePanel().setItems(items);
					
					ArrayList<Station> stations;
					if (item.name.equals(ALL)) {
						stations = sm.getAllStation();
					} else {
						stations = sm.getStationInZone(item.id);
					}
					items = new ArrayList<Item>();
					for (Station station : stations) {
						items.add(new Item(station.getId(), station.getName()));
					}
					getStationPanel().setItems(items);
				}
			});
		}
		return zonePanel;
	}
	
	private void initLinePanel() {
		ArrayList<Item> items = new ArrayList<Item>();
		BusLineManager lm = null;
		try {
			lm = BusLineManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<BusLine> lines = lm.getAllBusLine();
		for (BusLine line : lines) {
			Item item = new Item();
			item.id = line.getId();
			item.name = line.getName();
			items.add(item);
		}
		linePanel.setItems(items);
		linePanel.updateItemList();
	}
	
	private AutoCompletePanel getLinePanel() {
		if (linePanel == null) {
			linePanel = new AutoCompletePanel();
			linePanel.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, Color.BLACK));
			linePanel.setLabel("Chọn tuyến xe:");
			
			AutoCompletePanel linePanel = getLinePanel();
			
			
			linePanel.getItemList().addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {
					Item item = getLinePanel().getSelectedItem();
					if (item == null) return;
					ArrayList<Station> stations = null;
					ArrayList<Bus> buses = null;
					BusLine line = null;
					Company c = null;
					try {
						StationManager sm = StationManager.getInstance();
						BusLineManager blm = BusLineManager.getInstance();
						CompanyManager cm = CompanyManager.getInstance();
						BusManager bm = BusManager.getInstance();
						
						stations = sm.getStationInLine(item.id);
						line = blm.getBusLine(item.id);
						c = cm.getCompany(line.getCompanyId());
						buses = bm.getBuses(line.getId());
						
					} catch (Exception e1) {
					}
					String text = "Tuyến: " + item.name + "\n";
					if (line.getDetail() != null) {
						text += "Details: " + line.getDetail() + "\n";
					}
					if (c != null) {
						text += "Công ty quản lý: " + c.getName() + "\n";
					}
					text += "Đi qua các bến:\n";
					if (stations.size() > 0) {
						int size = stations.size();
						for (int i = 0; i < size - 1; i++) {
							text += stations.get(i).getName() + " - ";
						}
						text += stations.get(size - 1).getName();
					}
					text += "\nCác xe chạy trên tuyến:\n";
					if (buses.size() > 0) {
						EmployeeManager em = null;
						try {
							em = EmployeeManager.getInstance();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						int size = buses.size();
						for (int i = 0; i < size; i++) {
							Employee ee = em.getEmployee(buses.get(i).getEmployeeId());
							String name = "";
							if (ee != null) {
								name = "(" + ee.getName() + ")";
							}
							text += stations.get(i).getName() + name + " - ";
						}
						text = text.substring(0, text.length() -3);
					}
					getLineDetailTextArea().setText(text);
				}
			});
		}
		return linePanel;
	}
	
	private void initStationsPanel() {
		ArrayList<Item> items = new ArrayList<Item>();
		StationManager sm = null;
		try {
			sm = StationManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<Station> stations = sm.getAllStation();
		for (Station station : stations) {
			Item item = new Item();
			item.id = station.getId();
			item.name = station.getName();
			items.add(item);
		}
		//tab Tim duong
		getStationPanel().setItems(items);
		getStationPanel().updateItemList();
	}
	
	private AutoCompletePanel getStationPanel() {
		if (stationPanel == null) {
			stationPanel = new AutoCompletePanel();
			stationPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1, Color.BLACK));
			stationPanel.setLabel("Chọn bến xe:");
			
			stationPanel.getItemList().addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {
					Item item = getStationPanel().getSelectedItem();
					if (item == null) return;
					ArrayList<BusLine> lines = null;
					try {
						BusLineManager sm = BusLineManager.getInstance();
						lines = sm.getBusLineOverStation(item.id);
					} catch (SQLException e1) {
					}
					String text = "Bến: " + item.name + "\n";
					text += "Các tuyến đi qua:\n";
					if (lines.size() > 0) {
						int size = lines.size();
						for (int i = 0; i < size; i++) {
							text += "\t" + lines.get(i).getName() + "\n";
						}
						getStationDetailTextArea().setText(text);
					}
				}
			});
		}
		return stationPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(1);
			gridLayout1.setColumns(2);
			jPanel1 = new JPanel();
			jPanel1.setLayout(gridLayout1);
			jPanel1.add(getStartStation(), null);
			jPanel1.add(getEndStation(), null);
		}
		return jPanel1;
	}

	private void initStartStation() {
		ArrayList<Item> items = new ArrayList<Item>();
		StationManager sm = null;
		try {
			sm = StationManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<Station> stations = sm.getAllStation();
		for (Station station : stations) {
			Item item = new Item();
			item.id = station.getId();
			item.name = station.getName();
			items.add(item);
		}
		//tab Tim duong
		getStartStation().setItems(items);
		getStartStation().updateItemList();
	}
	
	/**
	 * This method initializes startStation	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private AutoCompletePanel getStartStation() {
		if (startStation == null) {
			startStation = new AutoCompletePanel();
			startStation.setLabel("Điểm đầu:");
			startStation.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
		}
		return startStation;
	}

	private void initEndStation() {
		ArrayList<Item> items = new ArrayList<Item>();
		StationManager sm = null;
		try {
			sm = StationManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<Station> stations = sm.getAllStation();
		for (Station station : stations) {
			Item item = new Item();
			item.id = station.getId();
			item.name = station.getName();
			items.add(item);
		}
		//tab Tim duong
		getEndStation().setItems(items);
		getEndStation().updateItemList();
	}
	
	/**
	 * This method initializes endStation	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private AutoCompletePanel getEndStation() {
		if (endStation == null) {
			endStation = new AutoCompletePanel();
			endStation.setLabel("Điểm cuối:");
			endStation.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, Color.BLACK));
		}
		return endStation;
	}

	/**
	 * This method initializes jPanel11	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel11() {
		if (jPanel11 == null) {
			jPanel11 = new JPanel();
			jPanel11.setLayout(new BorderLayout());
			jPanel11.add(getJPanel12(), BorderLayout.NORTH);
			jPanel11.add(new JScrollPane(getJTextArea()), BorderLayout.CENTER);
		}
		return jPanel11;
	}

	/**
	 * This method initializes jPanel12	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel12() {
		if (jPanel12 == null) {
			jPanel12 = new JPanel();
			jPanel12.setLayout(new BorderLayout());
			jPanel12.add(getFindButton(), BorderLayout.WEST);
		}
		return jPanel12;
	}

	/**
	 * This method initializes findButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getFindButton() {
		if (findButton == null) {
			findButton = new JButton("Tìm");
			findButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (getStartStation().getSelectedItem() != null && getEndStation().getSelectedItem() != null
							&& getStartStation().getSelectedItem().id != getEndStation().getSelectedItem().id) {
						try {
							StationManager sm = StationManager.getInstance();
							FindPath fp = FindPath.getInstance();
							Station start = sm.getStation(getStartStation().getSelectedItem().id);
							Station end = sm.getStation(getEndStation().getSelectedItem().id);
							if (start == null && end == null) return;
							ArrayList<Path> paths = fp.find(start, end);
							String text = "";
							if (paths.size() == 0) {
								text = "Ko tìm thấy đường.";
							}
							for (Path path : paths) {
								ArrayList<Station> stations = path.stations;
								ArrayList<Station> stations2 = path.stations2;
								if (path.noOfBusLine == 1) {
									text += path.line.getName() + "\n\t";
									for (int i = 0; i < stations.size() - 1; i++) {
										text += stations.get(i).getName() + " - ";
									}
									text += stations.get(stations.size() - 1).getName();
									text += "\n\n";
								} else if (path.noOfBusLine == 2) {
									text += "Đi 2 tuyến:";
									text += "\t" + path.line.getName() + "\n\t\t";
									for (int i = 0; i < stations.size() - 1; i++) {
										text += stations.get(i).getName() + " - ";
									}
									text += stations.get(stations.size() - 1).getName();
									text += "\n\t" + path.line2.getName() + "\n\t\t";
									for (int i = 0; i < stations2.size() - 1; i++) {
										text += stations2.get(i).getName() + " - ";
									}
									text += stations2.get(stations2.size() - 1).getName();
									text += "\n\n";
								}
							}
							getJTextArea().setText(text);
						} catch (Exception e1) {
							System.out.println("error");
						}
					}
				}
			});
		}
		return findButton;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setEditable(false);
			Font font = new Font(Font.SANS_SERIF, 0, 13);
			jTextArea.setFont(font);

		}
		return jTextArea;
	}

	/**
	 * This method initializes jPanel13	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel13() {
		if (jPanel13 == null) {
			jLabel = new JLabel();
			jLabel.setText("Chọn: ");
			jPanel13 = new JPanel();
			jPanel13.setLayout(new BorderLayout());
			jPanel13.add(jLabel, BorderLayout.WEST);
			jPanel13.add(getJComboBox(), BorderLayout.CENTER);
		}
		return jPanel13;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox(getTypeModel());
			jComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getAddPanel().remove(editPanel);
					getAddPanel().add(getEditPanel(getJComboBox().getSelectedIndex()));
					getAddPanel().updateUI();
				}
			});
		}
		return jComboBox;
	}
	
	private JPanel getEditPanel(int type) {
		switch(type) {
		case EDIT_LINE:
			//editPanel = getEditLinePanel();
			editPanel = new EditLinePanel();
			break;
		case EDIT_STATION:
			//editPanel = getEditStationPanel();
			editPanel = new EditStationPanel();
			break;
		case EDIT_ZONE:
			//editPanel = getEditZonePanel();
			editPanel = new EditZonePanel();
			break;
		}
		return editPanel;
	}
	
	private DefaultComboBoxModel getTypeModel() {
		if (typeModel  == null) {
			typeModel = new DefaultComboBoxModel();
		}
		return typeModel;
	}

	/**
	 * This method initializes editLinePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private EditLinePanel getEditLinePanel() {
		if (editLinePanel == null) {
			editLinePanel = new EditLinePanel();
			//editLinePanel.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black));
		}
		return editLinePanel;
	}
	
	private EditStationPanel getEditStationPanel() {
		if (editStationPanel == null) {
			editStationPanel = new EditStationPanel();
			//editLinePanel.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black));
		}
		return editStationPanel;
	}
	
	private EditZonePanel getEditZonePanel() {
		if (editZonePanel == null) {
			editZonePanel = new EditZonePanel();
			//editLinePanel.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black));
		}
		return editZonePanel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
		}
		return jButton;
	}

	/**
	 * This method initializes lineDetailTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getLineDetailTextArea() {
		if (lineDetailTextArea == null) {
			lineDetailTextArea = new JTextArea();
			lineDetailTextArea.setEditable(false);
			lineDetailTextArea.setLineWrap(true);
			lineDetailTextArea.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
		}
		return lineDetailTextArea;
	}

	/**
	 * This method initializes stationDetailTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getStationDetailTextArea() {
		if (stationDetailTextArea == null) {
			stationDetailTextArea = new JTextArea();
			stationDetailTextArea.setEditable(false);
			stationDetailTextArea.setLineWrap(true);
			stationDetailTextArea.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
		}
		return stationDetailTextArea;
	}

	/**
	 * Launches this application
	 */
	public static void main(String[] args) {
		try {
		    // Set cross-platform Java L&F (also called "Metal")
	        UIManager.setLookAndFeel(
	            UIManager.getSystemLookAndFeelClassName());
	    } 
	    catch (Exception e) {
	       // handle exception
	    }

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame application = new MainFrame();
				application.getJFrame().setVisible(true);
				
				
				
				AutoCompletePanel stationPanel = (AutoCompletePanel)(application.getStationPanel());

				/*ArrayList<Item> items = new ArrayList<Item>();
				StationManager sm = null;
				try {
					sm = StationManager.getInstance();
				} catch (SQLException e) {
				}
				ArrayList<Station> stations = sm.getAllStation();
				for (Station station : stations) {
					Item item = new Item();
					item.id = station.getId();
					item.name = station.getName();
					items.add(item);
				}
				//tab Tim duong
				stationPanel.setItems(items);
				stationPanel.updateItemList();
				
				application.getStartStation().setItems(items);
				application.getStartStation().updateItemList();
				application.getEndStation().setItems(items);
				application.getEndStation().updateItemList();*/
				
				
				
				
				//tab Them
				DefaultComboBoxModel typeModel = application.getTypeModel();
				typeModel.addElement("Tuyến");
				typeModel.addElement("Bến");
				typeModel.addElement("Khu vực");
			}
		});
	}

}